package com.yc.bos.web.action;

import com.yc.bos.model.Region;
import com.yc.bos.service.IRegionService;
import com.yc.bos.utils.PinYin4jUtils;
import com.yc.bos.web.action.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RegionAction extends BaseAction<Region> {
    @Autowired
    private IRegionService regionService;
    private File excelFile;

    public void setExcelFile(File excelFile) {
        this.excelFile = excelFile;
    }

    public String importExcel() throws IOException {
        System.out.println("excelFile:" + excelFile.getAbsolutePath());
        //1.解析excel文件中数据
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelFile));
        Sheet sheet = workbook.getSheetAt(0);
        List<Region> regions = new ArrayList<Region>();
        for (Row row : sheet) {
            String id = row.getCell(0).getStringCellValue();
            String province = row.getCell(1).getStringCellValue();
            String city = row.getCell(2).getStringCellValue();
            String district = row.getCell(3).getStringCellValue();
            String postcode = row.getCell(4).getStringCellValue();
            //根据中文生成城市编码
            String citycode = StringUtils.join(PinYin4jUtils.stringToPinyin(city), "");

            //根据中文生成简码
            String cityTmp = city.substring(0, city.length() - 1);
            String districtTmp = district.substring(0, district.length() - 1);
            String[] cityStrs = PinYin4jUtils.getHeadByString(cityTmp);
            String[] districtStrs = PinYin4jUtils.getHeadByString(districtTmp);
            String shortcode = StringUtils.join(cityStrs, "") + StringUtils.join(districtStrs, "");
            //封装成Region模型
            Region region = new Region(id, province, city, district, postcode);
            region.setCitycode(citycode);
            region.setShortcode(shortcode);
            regions.add(region);
            System.out.println("------------");
        }
        regions.remove(0);
        regionService.saveAll(regions);
        responseStr("success");
        return NONE;
    }

    @Override
    public String save() {
        return null;
    }

    @Override
    public String update() {
        return null;
    }

    @Override
    public String delete() throws IOException {
        return null;
    }

    @Override
    public String list() {
        return null;
    }

    public void pageQuery() throws IOException {
        pd.setCurrentPage(page);
        pd.setPageSize(rows);
        regionService.pageQuery(pd);
        responseJson(pd, new String[]{"currentPage", "pageSize", "detachedCriteria", "subareas" });
    }

    public void listJson() throws IOException {
        List<Region> regions = regionService.findAll();
        /**
         * //返回json数据
         *转json不要转subareas字段，否则会出现死循环
         */
        responseJson(regions, new String[]{"subareas" });
    }
}